package com.dragon.app.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.IWorkflowService;
import com.dragon.repository.domain.dto.WorkflowDTO;
import com.dragon.repository.domain.vo.WorkflowVO;
import com.dragon.repository.entity.Workflow;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 工作流配置表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-04
 */
@RestController
@RequestMapping("workflow")
@Api(tags = "工作流")
public class WorkflowController {

    @Autowired
    IWorkflowService service;

    @GetMapping("pageList")
    @ApiOperation(value = "分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",required = false,type = "int",value = "类型列表"),
            @ApiImplicitParam(name = "name",required = false,type = "String",value = "类型列表"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "类型列表"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "类型列表"),
    })
    public ResponseData<List<WorkflowDTO>> selectPageList(
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String name,
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Workflow> workflowPage = service.selectPageList(type, name, page, limit);
        return ResponseData.success(WorkflowDTO.convertList(workflowPage.getRecords()),workflowPage.getTotal());
    }


    @PostMapping("addWorkflow")
    @ApiOperation(value = "添加工作流")
    public ResponseData addWorkflow(@RequestBody @Validated(Insert.class)WorkflowVO vo){
        service.addWorkflow(WorkflowVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("updateWorkflow")
    @ApiOperation(value = "修改工作流")
    public ResponseData updateWorkflow(@RequestBody @Validated(Update.class)WorkflowVO vo){
        service.updateWorkflow(WorkflowVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("deleteOrDeleteBatch")
    @ApiOperation(value = "删除或者批量删除")
    public ResponseData deleteOrDeleteBatch(
            @RequestParam
            @ApiParam(name = "ids",required = true,type = "string",value = "删除id集合") String ids){
        service.deleteWorkflow(ids);
        return ResponseData.success();
    }

    @GetMapping("one")
    @ApiOperation("获取工作流配置")
    public ResponseData selectWorkflowById(@RequestParam @ApiParam(name = "id",required = true,type = "int",value = "流程类型") Integer id){
        return ResponseData.success(WorkflowDTO.convert(service.selectWorkflowById(id)));
    }


    @GetMapping("list")
    @ApiOperation(value = "获取对应流程的列表")
    public ResponseData<List<WorkflowDTO>> selectListByType(
            @RequestParam @ApiParam(name = "type",required = true,type = "int",value = "流程类型") Integer type
    ){
        List<Workflow> result = service.selectListByType(type);
        return ResponseData.success(WorkflowDTO.convertList(result));
    }
}
